package com.ewanghuiju.app.presenter.my;

import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.RxPresenter;
import com.ewanghuiju.app.base.contract.my.WalletRechargeContract;
import com.ewanghuiju.app.component.RxBus;
import com.ewanghuiju.app.model.DataManager;
import com.ewanghuiju.app.model.bean.response.PaySignResponBean;
import com.ewanghuiju.app.model.event.SendEvent;
import com.ewanghuiju.app.model.http.response.MyHttpResponse;
import com.ewanghuiju.app.model.http.response.Optional;
import com.ewanghuiju.app.pay.PayReslut;
import com.ewanghuiju.app.util.RxUtil;
import com.ewanghuiju.app.widget.CommonSubscriber;

import javax.inject.Inject;

import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Predicate;

/**
 * @author sj  qq:751943509
 * @class 钱包充值
 * @time 2020/2/14
 */
public class WalletRechargePresenter extends RxPresenter<WalletRechargeContract.View> implements WalletRechargeContract.Presenter {
    private DataManager mDataManager;

    @Inject
    public WalletRechargePresenter(DataManager mDataManager) {
        this.mDataManager = mDataManager;
    }

    @Override
    public void getData() {

    }

    /**
     * 获取支付签名
     */
    @Override
    public void getPaySign(String payType, String pay_price) {
        addSubscribe(mDataManager.userpay(payType, pay_price)
                        .compose(RxUtil.<MyHttpResponse<PaySignResponBean>>rxSchedulerHelper())
                        .compose(RxUtil.handleMyResultCannull())
                        .subscribeWith(new CommonSubscriber<Optional<PaySignResponBean>>(mView) {
                            @Override
                            public void onNext(Optional<PaySignResponBean> paySignResponBeanOptional) {
//                                mView.showLogin(paySignResponBeanOptional.getIncludeNull());
                                if (mView != null) {
                                    mView.getPaySignSuccess(paySignResponBeanOptional.getIncludeNull());
                                }
                            }

                            @Override
                            public void onError(Throwable e) {
                                super.onError(e);
                            }
                        })
        );
    }

    @Override
    public void attachView(WalletRechargeContract.View view) {
        super.attachView(view);
        registerEvent();
    }

    /**
     * 支付回调
     */
    private void registerEvent() {
        addSubscribe(RxBus.getDefault().toFlowable(SendEvent.class)
                .compose(RxUtil.<SendEvent>rxSchedulerHelper())
                .filter(new Predicate<SendEvent>() {
                    @Override
                    public boolean test(@NonNull SendEvent sendEvent) throws Exception {
                        return sendEvent.getType() == Constants.TYPE_PAY_STATUS_RETURN;
                    }
                })
                .subscribeWith(new CommonSubscriber<SendEvent>(mView, "") {
                    @Override
                    public void onNext(SendEvent s) {
                        PayReslut payReslut = (PayReslut) s.getData();
                        if (mView != null) {
                            mView.showPaySuccess(payReslut);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        registerEvent();
                    }
                })
        );
    }
}
